package com.lordjoe.collectiveintelligence.data.ui;

import org.jfree.chart.*;
import org.jfree.ui.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.labels.*;
import org.jfree.chart.plot.*;
import org.jfree.data.category.*;
import org.jfree.chart.renderer.category.*;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * com.lordjoe.collectiveintelligence.data.ui.PointPlot
 *
 * @author Steve Lewis
 * @date May 24, 2009
 */
public class PointPlot extends JPanel
{
    public static PointPlot[] EMPTY_ARRAY = {};
    public static Class THIS_CLASS = PointPlot.class;
    public static final Random RND = new Random();

    private CategoryScatterPlot m_Chart;
    private String m_XLabel;
    private String m_YLabel;


    /**
     * Creates a new <code>JPanel</code> with a double buffer
     * and a flow layout.
     */
    public PointPlot(String title,String xLabel, String yLabel,DPoint[]... data)
    {
        m_XLabel = xLabel;
        m_YLabel = yLabel;
        setLayout(new BorderLayout());
        final NumberAxis domainAxis = new NumberAxis(xLabel);
        domainAxis.setAutoRangeIncludesZero(false);
        final NumberAxis rangeAxis = new NumberAxis(yLabel);
        rangeAxis.setAutoRangeIncludesZero(false);

        m_Chart = new CategoryScatterPlot(domainAxis,rangeAxis,data);
        final JFreeChart chart = new JFreeChart(title, m_Chart);
        final ChartPanel panel = new ChartPanel(chart, true);
        panel.setPreferredSize(new java.awt.Dimension(600, 450));
        add(panel, BorderLayout.CENTER);
    }

    public static DPoint[] buildDataSet(int n)
    {
        List<DPoint> holder = new ArrayList<DPoint>();
        for (int i = 0; i < n; i++) {
            DPoint added = new DPoint(RND.nextDouble(),RND.nextDouble());
            holder.add(added);
        }
        DPoint[] ret = new DPoint[holder.size()];
        holder.toArray(ret);
        return ret;
    }

    /**
     * Creates a new <code>JPanel</code> with a double buffer
     * and a flow layout.
     */
    public static void showPlot(String title,String xLabel, String yLabel,DPoint[]... data)
    {
        JFrame jf = new JFrame();
        PointPlot pp = new PointPlot(title, xLabel,  yLabel, data);
        jf.setLayout(new BorderLayout());
        jf.add(pp, BorderLayout.CENTER);
        jf.pack();
        jf.setTitle("Plot");
        jf.setVisible(true);
    }

    public static void main(String[] args)
    {
        showPlot("Age Effect","Male Age","Female Age",
               buildDataSet(100), buildDataSet(50) );
       
    }

}
